package net.cruciblesoftware.ghostspacekillah;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.Rect;

class PlayerShip extends Ship {

    private int minVel;
    private float responseTime;

    private float shotTime, shotDelayTime;

    public PlayerShip() {
        super(Ship.TYPE_PLAYER);
        frameStepTime = Settings.getScaled("player_frame_step_time");
        logi("player_frame_step_time = " + frameStepTime);

        // get movement settings, set position
        minVel = (int)(SceneView.width * 
                       Settings.getPercent("player_min_vel"));
        responseTime = Settings.getPercent("player_resp_time");
        posX = rand.nextInt(SceneView.width - imgWidth) + (imgWidth / 2);
        posY = (SceneView.height * 
                Settings.getPercent("player_pos_y"));
        rotation = 0;

        // get shooting settings
        shotDelayTime = Settings.getPercent("bullet_delay_time");
        shotTime = 0.0f;

        // prevent movement before user touch event
        SceneView.lastTouchX = posX;
    }


    @Override
    public void update(float elapsed) {
        super.update(elapsed);

        // movement
        float lastTouchX = SceneView.lastTouchX;
        float distToTarget = lastTouchX - posX;
        float vel = Math.max(Math.abs(distToTarget/responseTime), minVel);
        if(Math.abs(distToTarget) < 6.0) {
            posX = lastTouchX;
            rotation = 0;
        } else if(distToTarget < 0.0f) {
            // move to the left
            posX -= vel * elapsed;
            float angle = (float)Math.atan(distToTarget / imgHeight);
            rotation = (int)(Math.toDegrees(angle));
        } else if(distToTarget > 0.0f) {
            // move to the right
            posX += vel * elapsed;
            float angle = (float)Math.atan(distToTarget / imgHeight);
            rotation = (int)(Math.toDegrees(angle));
        }

        // clamp position to screen bounds
        if(posX < 0.0f) posX = 0.0f;
        if(posX > (float)SceneView.width) posX = (float)SceneView.width;

        // shooting
        shotTime += elapsed;
        if(shotTime >= shotDelayTime) {
            float bX = posX + (float)(Math.sin(Math.toRadians(rotation)) * (imgWidth/2));
            float bY = posY - (imgHeight/2);
            while(shotTime >= shotDelayTime) {
                PlayingField.getInstance().addBullet(bX, bY, rotation);
                shotTime -= shotDelayTime;
            }
        }
    }


    public void shotDelayMultiplier(float m) {
        shotDelayTime *= m;
    }


    @Override
    public void draw(Canvas c) {
        int w_2 = imgWidth / 2;
        int h_2 = imgHeight / 2;
        src.right = imgWidth;
        src.bottom = imgHeight;
        dest.left = (int)(posX - w_2);
        dest.top = (int)(posY - h_2);
        dest.right = (int)(posX + w_2);
        dest.bottom = (int)(posY + h_2);
        c.save();
        c.rotate((float)rotation, posX, posY);
        c.drawBitmap(img, src, dest, null);
        c.restore();
    }


    /* Logging functions */
    private void logi(String msg) {
        android.util.Log.i(this.getClass().getName(), msg);
    }
    private void logw(String msg) {
        android.util.Log.w(this.getClass().getName(), msg);
    }
    private void loge(String msg) {
        android.util.Log.e(this.getClass().getName(), msg);
    }
}
